package com.xyx.ssm.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xyx.ssm.po.SyRole;
import com.xyx.ssm.po.Syuser;
import com.xyx.ssm.po.Syuserrole;
import com.xyx.ssm.service.SyroleService;
import com.xyx.ssm.service.SyuserService;
import com.xyx.ssm.util.Page;

@Controller
@RequestMapping(value = "/syuser")
public class SyuserAction {
	@Resource
	private SyuserService syuserService;
	@Resource
	private SyroleService syroleService;

	@RequestMapping("/pageView")
	public String pageView(Model model, Integer pageNow, String pageName,
			String departName) {
//		Map<String, Object> map = new HashMap<String, Object>();
//		// 用户名
//		map.put("pageName", pageName); 
//		if (pageNow == null) {
//			pageNow = 1;
//		}
//		System.out.println(map.toString());
//		// 1:当前页码
//		// 1:查询参数
//		Page<Map<String, Object>> page = new Page<Map<String, Object>>(pageNow,
//				map, 2);
//		Page<Map<String, Object>> page1 = syuserService.findPage(page);
//		page1.setParam(map);
//		model.addAttribute("page", page1);
		return "user/syuser";
	}

	@RequestMapping("/jsons")
	@ResponseBody
	public Map<String, Object> jsons(Model model, Integer iDisplayLength,
			Integer sEcho, Integer iDisplayStart, String pageName,
			String departName) {

		Map<String, Object> map111 = new HashMap<String, Object>();
		// 用户名
		map111.put("pageName", "");
		if (sEcho == null) {
			sEcho = 1;
		}
		if (iDisplayLength == null) {
			iDisplayLength = 10;
		}
		if (iDisplayStart == null) {
			iDisplayStart = 1;
		}
		System.out.println(map111.toString());
		// 1:当前页码
		// 1:查询参数
		Page<Map<String, Object>> page = new Page<Map<String, Object>>(
				iDisplayStart, map111, iDisplayLength);
		Page<Map<String, Object>> page1 = syuserService.findPage(page);
		System.out.println(page1);
		int num = page1.getList().size();
		Map<String, Object> map = new HashMap<String, Object>();
		List<Ceshi> list = new ArrayList<Ceshi>();
		for (int i = 0; i < num; i++) {
			Ceshi ceshi = new Ceshi();
			ceshi.setId(i);
			ceshi.setUsername(page1.getList().get(i).get("id").toString());
			list.add(ceshi);
		}
		map.put("sEcho", sEcho);
		map.put("iTotalRecords", page1.getTotalCount());
		map.put("iTotalDisplayRecords", page1.getTotalCount());
		map.put("aaData", page1.getList());
		return map;
	}

	@RequestMapping("/syuserinsertadd")
	@ResponseBody
	public Map<String, Object> syuseradd(Syuser syuser) {
		int num = syuserService.syuseradd(syuser);
		Map<String, Object> mapjson = new HashMap<String, Object>();
		if (num > 0) {
			mapjson.put("flag", true);
			mapjson.put("msg", "添加成功！");
		} else {
			mapjson.put("flag", false);
			mapjson.put("msg", "添加失败！");
		}
		return mapjson;
	}

	@RequestMapping("/syuserdelete")
	@ResponseBody
	public Map<String, Object> syuserdelete(String ids) {

		int num = syuserService.syuserDelete(ids);
		Map<String, Object> mapjson = new HashMap<String, Object>();
		if (num > 0) {
			mapjson.put("flag", true);
			mapjson.put("msg", "删除成功！");
		} else {
			mapjson.put("flag", false);
			mapjson.put("msg", "删除失败！");

		}
		return mapjson;
	}

	@RequestMapping("/syuserdetail")
	@ResponseBody
	public Map<String, Object> syuserdetail(Integer id) {

		System.out.println("so easy -" + id);
		List<Syuser> list = syuserService.syuserId(id);
		System.out.println("-------------------------------:" + list);
		Map<String, Object> mapjson = new HashMap<String, Object>();

		mapjson.put("name", list.get(0).getName());
		mapjson.put("password", list.get(0).getPassword());
		mapjson.put("id", id);
		mapjson.put("createtime", list.get(0).getCreatetime());
		mapjson.put("realname", list.get(0).getRealname());
		mapjson.put("phone", list.get(0).getPhone());
		mapjson.put("camera", list.get(0).getCamera());
		mapjson.put("birthday", list.get(0).getBirthday());
		mapjson.put("state", list.get(0).getState());
		mapjson.put("address", list.get(0).getAddress());
		mapjson.put("bm", list.get(0).getBm());
		mapjson.put("email", list.get(0).getEmail());
		mapjson.put("zw", list.get(0).getZw());

		return mapjson;
	}

	@RequestMapping("/syuserupdate")
	@ResponseBody
	public Map<String, Object> syuserUpdate(Syuser syuser) {
		int num = syuserService.syuserUpdate(syuser);
		Map<String, Object> mapjson = new HashMap<String, Object>();
		if (num > 0) {
			mapjson.put("flag", true);
			mapjson.put("msg", "修改成功！");
		} else {
			mapjson.put("flag", false);
			mapjson.put("msg", "修改失败！");
		}

		return mapjson;
	}
	
	/**
	 * 给用户添加角色
	 */
		@RequestMapping("/insertuserrole")
		@ResponseBody
		public Map<String, Object> insertuserrole(String ids) {
			String[] idarray = ids.split(",");
			int userid=Integer.parseInt(idarray[0]);
			int num = 1;
			syroleService.deleteall(userid);
			for (int i=0;i<idarray.length;i++) {
				int id = Integer.parseInt(idarray[i]);
				Syuserrole ur=new Syuserrole();
				if(i!=0){
					
					ur.setRoleid(id);
					ur.setUserid(userid);				
					num = syuserService.insertuserrole(ur);
					}
			}
			Map<String, Object> mapjson = new HashMap<String, Object>();
			if (num > 0) {
				mapjson.put("flag", true);
				mapjson.put("msg", "授权成功！");
			} else {
				mapjson.put("flag", false);
				mapjson.put("msg", "授权失败！");
			}
			return mapjson;
		}
		
		@RequestMapping("/jsonrole")
		@ResponseBody
		public Map<String, Object> jsonrole(HttpSession session,Integer id){
			
			List<SyRole> list=syroleService.selallrole();
			session.setAttribute("rolelist", list);
			Map<String, Object> json=new HashMap<String, Object>();
			json.put("id", id);
					return json;
		}

}
